use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
cd "${output}"

encode RACE, gen(race)
gen race_string="other" if race==1
replace race_string="black" if race==2
replace race_string="hispanic" if race==3
replace race_string="other" if race==4
replace race_string="white" if race==5

**Restrict and standardize to adolescent pop
keep if age>=13
*****************

global neg_loadf1 "B10_14	B10_17	B10_15	B10_13"

**REVERSE CODE negative loaders**
foreach  i in $neg_loadf1 {
gen RE`i'=5 if `i'==1
replace RE`i'=4 if `i'==2
replace RE`i'=3 if `i'==3
replace RE`i'=2 if `i'==4
replace RE`i'=1 if `i'==5
}

global f1 "B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7"
global f2 "REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18"
global f3 "B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe"
global f4 "C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol"

*************
**Standardize to pooled sample**
*********
foreach x in comp_mental_health $f1 $f2 $f3 $f4 {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

egen rules=rowmean(B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7)
egen enforce=rowmean(REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18)
egen relate=rowmean(B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe)
egen adverse=rowmean(C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol)

*Parenting items
alpha B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7  REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18
alpha B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7 
alpha REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18
*Relationship items
alpha B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol
alpha B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe
alpha C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol

foreach x in rules enforce  relate adverse  {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
} 
label var rules "Responsiveness"
label var enforce "Demandingness"
label var relate "Relationship quality"
label var adverse "Absence of traumatic experiences"

global controls "male age_group_num1 age_group_num2 age_group_num3 age_group_num4 age_group_num5 age_group_num6 age_group_num7 age_group_num8"

bysort age: sum comp_mental_health [aw=weight]

**TABLE 7
**Calculate correlations to compare to meta-analysis and test for genetic confounds
pwcorr comp_mental_health rules enforce adverse relate [aw=child_weight], obs
pwcorr comp_mental_health rules enforce adverse relate [aw=weight], obs
pwcorr comp_mental_health rules enforce adverse relate if bio_parent==1 [aw=weight], obs
pwcorr comp_mental_health rules enforce  adverse  relate if bio_parent!=1 [aw=weight], obs
pwcorr comp_mental_health rules enforce  adverse  relate if bio_parent!=1 & grandparent!=1 & family_mem!=1 [aw=weight], obs


************
**Evocative effects, Supplemental Table 5
***********
pwcorr relate rules enforce agreeableness courage conscientiousness emo_stability extro openness fairness social_skill wisdom smart ///
bullied B11E B12_activities B13_time_spent [aw=weight]

gen a=relate
gen b=rules
gen c=enforce
gen d=adverse
pwcorr  a b  [aw=weight]
gen rab=r(rho)
pwcorr  a c  [aw=weight]
gen rac=r(rho)
pwcorr  a d  [aw=weight]
gen rad=r(rho)
pwcorr  b c  [aw=weight]
gen rbc=r(rho)
pwcorr  b d  [aw=weight]
gen rbd=r(rho)
pwcorr  c d  [aw=weight]
gen rcd=r(rho)

estimates clear

eststo: reg  relate  rules enforce  adverse   $controls  [aw=weight]
eststo:  reg  relate  rules enforce  adverse   $controls agreeableness B11E  conscientiousness   [aw=weight]
eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls  [aw=weight]
nlcom _b[relate]+_b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rab*_b[relate]*_b[rules]) + ///
(rac*_b[relate]*_b[enforce]) + ///
(rad*_b[relate]*_b[adverse]) + ///
(rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
*.826

eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls agreeableness B11E  conscientiousness  [aw=weight]
nlcom _b[relate]+_b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rab*_b[relate]*_b[rules]) + ///
(rac*_b[relate]*_b[enforce]) + ///
(rad*_b[relate]*_b[adverse]) + ///
(rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
*.64
di .64/.826
di .64^2
di .826^2

esttab using "Evocative_Effects.csv", ///
stats(r2_a N, fmt(%9.3f %9.0g) label("Adjusted R-squared" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)   nogaps replace 

*********
**Genetic confounds, Sup Table 6
estimates clear

eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls  if bio_parent==1  [aw=weight]
eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls  if bio_parent!=1  [aw=weight]
eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls agreeableness B11E  conscientiousness if bio_parent==1  [aw=weight]
eststo:  reg  comp_mental_health relate  rules enforce  adverse   $controls agreeableness B11E  conscientiousness if bio_parent!=1  [aw=weight]

esttab using "Genetic_and_Evocative_Effects.csv", ///
stats(r2_a N, fmt(%9.3f %9.0g) label("Adjusted R-squared" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)  mtitle("Biological parents" "Non-biological parents" "Biological parents" "Non-biological parents")  nogaps replace 


***Parent vs child report, Sup Table 8
pwcorr  comp_mental_health p_health_inde c_health_index relate B6_overall_parent_ch  C19_parent_love rules enforce  adverse p_objective_health  [aw=weight]

label var p_objective_health "Parent report of objective measures of child's mental health problems"
label var comp_mental_health "Index of child's mental health"
label var p_health_index "Parent reported: index of child's health"
label var c_health_index "Child reported: index of mental and physical health"

Mental health index
Parent-reported youth mental health index
Child reported-youth mental health index
Relationship quality
Parent reported relationship quality
Youth-reported relationship quality
Responsiveness (parent reported)
Demandingness (parent reported)
Absence of childhood traumatic experiences (adolescent reported)
Parent's objective report of youth's mental health


Relationship quality
Parent reported relationship quality
Youth-reported relationship quality
Responsiveness (parent reported)
Demandingness (parent reported)
Absence of childhood traumatic experiences (adolescent reported)
Parent's objective report of youth's mental health

Youth-reported relationship quality
Responsiveness (parent reported)
Demandingness (parent reported)
Absence of childhood traumatic experiences (adolescent reported)
Parent's objective report of youth's mental health

	 B9_relationship_othe REB10_8 ///